package com.appoint.buss.service.impl;

import java.util.List;

import com.appoint.buss.domain.EpiparaRnaInfo;
import com.appoint.buss.mapper.EpiparaInfoMapper;
import com.appoint.common.exception.ServiceException;
import com.appoint.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.appoint.buss.mapper.EpiparaRnaSeqMapper;
import com.appoint.buss.domain.EpiparaRnaSeq;
import com.appoint.buss.service.IEpiparaRnaSeqService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * rna_seqService业务层处理
 *
 * @author appoint
 * @date 2023-01-10
 */
@Service
public class EpiparaRnaSeqServiceImpl  extends ServiceImpl<EpiparaRnaSeqMapper,EpiparaRnaSeq>  implements IEpiparaRnaSeqService
{
    @Resource
    private EpiparaRnaSeqMapper epiparaRnaSeqMapper;

    @Resource
    private EpiparaInfoMapper epiparaInfoMapper;

    /**
     * 查询rna_seq
     *
     * @param id rna_seq主键
     * @return rna_seq
     */
    @Override
    public EpiparaRnaSeq selectEpiparaRnaSeqById(Long id)
    {
        return epiparaRnaSeqMapper.selectEpiparaRnaSeqById(id);
    }

    /**
     * 查询rna_seq列表
     *
     * @param epiparaRnaSeq rna_seq
     * @return rna_seq
     */
    @Override
    public List<EpiparaRnaSeq> selectEpiparaRnaSeqList(EpiparaRnaSeq epiparaRnaSeq)
    {
        return epiparaRnaSeqMapper.selectEpiparaRnaSeqList(epiparaRnaSeq);
    }

    /**
     * 新增rna_seq
     *
     * @param epiparaRnaSeq rna_seq
     * @return 结果
     */
    @Override
    public int insertEpiparaRnaSeq(EpiparaRnaSeq epiparaRnaSeq)
    {
        return epiparaRnaSeqMapper.insertEpiparaRnaSeq(epiparaRnaSeq);
    }

    /**
     * 修改rna_seq
     *
     * @param epiparaRnaSeq rna_seq
     * @return 结果
     */
    @Override
    public int updateEpiparaRnaSeq(EpiparaRnaSeq epiparaRnaSeq)
    {
        return epiparaRnaSeqMapper.updateEpiparaRnaSeq(epiparaRnaSeq);
    }

    /**
     * 批量删除rna_seq
     *
     * @param ids 需要删除的rna_seq主键
     * @return 结果
     */
    @Override
    public int deleteEpiparaRnaSeqByIds(Long[] ids)
    {
        return epiparaRnaSeqMapper.deleteEpiparaRnaSeqByIds(ids);
    }

    /**
     * 删除rna_seq信息
     *
     * @param id rna_seq主键
     * @return 结果
     */
    @Override
    public int deleteEpiparaRnaSeqById(Long id)
    {
        return epiparaRnaSeqMapper.deleteEpiparaRnaSeqById(id);
    }

    @Override
    public String importUser(List<EpiparaRnaSeq> list, Boolean isUpdateSupport, String operName,String fileName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        EpiparaRnaInfo epiparaInfoReq = new EpiparaRnaInfo();
        epiparaInfoReq.setIndex(fileName);
        List<EpiparaRnaInfo> infos = epiparaInfoMapper.selectEpiparaInfoList(epiparaInfoReq);
        String fileDownPath = "";
        if(!CollectionUtils.isEmpty(infos)){
            EpiparaRnaInfo info = infos.get(0);
            fileDownPath = info.getSpecies() + "/RNA-seq/" + info.getAccession() + "/8.differential_analysis/" + info.getIndex() + ".xlsx";
        }
        for (EpiparaRnaSeq info : list) {
            info.setFileName(fileName);
            info.setFileDownPath(fileDownPath);
        }
        String resultMsg = "恭喜您，数据已全部导入成功！共 " + list.size() + " 条";
        try {
            this.saveBatch(list);
        } catch (Exception e) {
            resultMsg  = "很抱歉，导入失败！";
            log.error(resultMsg, e);
        }
        return resultMsg;
    }
}
